<?php
namespace app\index\model;
use think\Model;
class Cart extends Model
{
   public function addToCart($goodsId,$goodsAttr = '',$goodsNum = 1){
	   //1-23,24-123
	   $cart=isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) :array();
		$key = $goodsId.'-'.$goodsAttr;
		if(isset($cart[$key])){
			$cart[$key] += $goodsNum; //如果在此之前已经为该商品加入了购物车,那么再次添加需要修改商品数量
		}else{
			$cart[$key] = $goodsNum;
		}
		$aMonth = time()+30*24*3600;
		setcookie('cart',serialize($cart),$aMonth,'/');
   }
   //清空购物车
   public function clearCart(){
	   setcookie('cart','',1,'/');
   }
   //删除一条购物车记录
   public function delCart($idAttr){
	   $cart = isset($_COOKIE['cart'])? unserialize($_COOKIE['cart']):array();
	   $key = $idAttr;
	   unset($cart[$key]);
	   $aMonth = time()+30*24*3600;
	   setcookie('cart',serialize($cart),$aMonth,'/');
   }
   //删除多条购物车记录
   public function deleteCartGoods($cartvalue){
	   $cart=isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) : array();
	   $cartValue=explode('@', $cartvalue);
	   // [
	   //    '16-74,76',
	   //    '15-68,69'
	   // ]
	   foreach ($cartValue as $k => $v) {
	      unset($cart[$v]);
	   }
	   $aMonth=time()+30*24*3600;
	   setcookie('cart',serialize($cart),$aMonth,'/');
   }
   //修改购物车中的商品数量
   public function updateCart($idAttr,$goodsNum){
	   $cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) : array();
		$key = $idAttr;
		$cart[$key]=$goodsNum;
		$aMonth=time()+30*24*3600;
		setcookie('cart',serialize($cart),$aMonth,'/');
		
   }
   //计算下单的商品总价格
   public function doGoodsPriceCount($doGoods){
	$goodsTotalPrice = 0;
	$goods=model('goods');
	$cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) :array();
	if($doGoods){   
		$doGoodsArr = explode('@',$doGoods);
		foreach ($cart as $k => $v){
			if(!in_array($k,$doGoodsArr)){
				unset($cart[$k]);
			}
		}
		$_cart = array();
		foreach($cart as $k => $v){
			$arr = explode('-',$k); //$arr[0] 就是商品的id  如果存在第二个元素的话$arr[1] 代表的商品的单选属性id字符串
			$memberPrice = $goods->getMemberPrice($arr[0]);
			$_cart[$k]['member_price']=$memberPrice;
			if($arr[1]){
				$goodsAttrPrice = 0;
				$goodsAttrRes = db('goods_attr')->alias('ga')->field('ga.*,attr_name')->join('attr a ','ga.attr_id = a.id')->where('ga.id','in',$arr[1])->select();
			 foreach($goodsAttrRes as $k1 => $v1){
				 $goodsAttrPrice += $v1['attr_price'];
			 }
			 $_cart[$k]['member_price'] += $goodsAttrPrice; //计算商品属性价格累计
			}
			$goodsTotalPrice +=$_cart[$k]['member_price']*$v;  //计算登录后的会员价格和属性价格
		}
	}
		return $goodsTotalPrice;
   }
   //读取cookie获取购物车商品
   public function getGoodsListCart($doGoods=''){
	  $goods=model('goods');
	   $cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) :array();
	   if($doGoods){
		   $doGoodsArr = explode('@',$doGoods);
		   foreach ($cart as $k => $v){
			   if(!in_array($k,$doGoodsArr)){
				   unset($cart[$k]);
			   }
		   }
	   }
	   // dump($cart);die;
	   $_cart = array();
	   foreach($cart as $k => $v){
		   $arr = explode('-',$k); //$arr[0] 就是商品的id  如果存在第二个元素的话$arr[1] 代表的商品的单选属性id字符串
		   $goodsInfo = $goods->field('id,goods_name,mid_thumb,shop_price,markte_price')->find($arr[0]);
		   $memberPrice = $goods->getMemberPrice($arr[0]);
		   $_cart[$k]['goods_name'] = $goodsInfo['goods_name'];
		   $_cart[$k]['mid_thumb'] = $goodsInfo['mid_thumb'];
		   $_cart[$k]['member_price']=$memberPrice;
		   $_cart[$k]['goods_num']=$v;
		   $_cart[$k]['shop_price']=$goodsInfo['shop_price'];
		   $_cart[$k]['market_price']=$goodsInfo['markte_price'];
			$_cart[$k]['goods_id']=$goodsInfo['id'];
		   $_cart[$k]['goods_id_attr_id']=$k;
		   $_cart[$k]['goods_attr_str']='';
		   
		   if($arr[1]){
			   //属性名称   属性值     属性价格
			   //颜色      红色        0
			   //尺寸      XXL         100
			   $goodsAttrStr = array(); //商品单选属性字符串
			   $goodsAttrPrice = 0;
			   $goodsAttrRes = db('goods_attr')->alias('ga')->field('ga.*,attr_name')->join('attr a ','ga.attr_id = a.id')->where('ga.id','in',$arr[1])->select();
			foreach($goodsAttrRes as $k1 => $v1){
				$goodsAttrStr[] = $v1['attr_name'].''.$v1['attr_value'].'(￥'.$v1['attr_price'].'元)';
				$goodsAttrPrice += $v1['attr_price'];
			}
			$goodsAttrStr = implode('<br />',$goodsAttrStr);
			
			$_cart[$k]['goods_attr_str']=$goodsAttrStr;
			$_cart[$k]['member_price'] += $goodsAttrPrice;
			$_cart[$k]['shop_price'] += $goodsAttrPrice;
		   }
		    $_cart[$k]['subtotal']=$_cart[$k]['member_price']*$v;
	   }
	 
	   return $_cart;
   }
   //购物车数据改动时,计算选中的商品的总价格,节省价格,总数
   //$recId为选中的商品的id字符串:1,2,3
   public function ajaxCartGoodsAmount($recId){
	   
	   $goods=model('goods');
	   $_cart['subtotal_number']= 0; //商品总数
	   $_cart['goods_amount'] = 0;//商品会员价总金额
	   $_cart['save_total_amount'] = 0; //优惠节省总额
	   $_cart['shop_total'] = 0; //商品本店价总金额
	   
	   $recIdArr = explode('@',$recId);
	   $cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) :array();
	   //删除为选定的购物车中的商品
	   foreach($cart as $k => $v){
		   //$arr = explode('-',$k);  //$arr[0] 商品id ,如果存在第一个元素的话$arr[1]代表商品单选属性id字符串
		   if(!in_array($k,$recIdArr)){
			   unset($cart[$k]);
		   }
	   }
	   //开始计算
	   foreach($cart as $k1 =>$v1){
		   //计算商品总数
		   $_cart['subtotal_number'] += $v1;
		   
		   $arr=explode('-',$k1); 
		   //计算商品总会员价格
		    $memberPrice = $goods->getMemberPrice($arr[0]);
			//计算商品总本店价(含属性价格)
			$shopPrice = $goods->getShopPrice($arr[0]);
		   if($arr[1]){
			$goodsAttrPrice = 0;
			$goodsAttrRes = db('goods_attr')->field('attr_price')->where('id','in',$arr[1])->select();
			foreach($goodsAttrRes as $k2 => $v2){
				$goodsAttrPrice += $v2['attr_price'];
			}
		   		$memberPrice += $goodsAttrPrice;
				$shopPrice += $goodsAttrPrice;
		   }
		   $_cart['goods_amount'] +=$memberPrice*$v1;
		   $_cart['shop_total'] +=$shopPrice*$v1;
	   }
	   //计算商品总节省
	   $_cart['save_total_amount'] = $_cart['shop_total']-$_cart['goods_amount'];
	   return $_cart;
   }
}
